package com.lyj.linearalgebra.matrix.web.servlet;

import cn.itcast.servlet.BaseServlet;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyj.linearalgebra.matrix.domain.Matrix;
import com.lyj.linearalgebra.matrix.service.MatrixException;
import com.lyj.linearalgebra.matrix.service.MatrixService;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * Servlet implementation class MatrixServlet
 */
@WebServlet("/MatrixServlet")
public class MatrixServlet extends BaseServlet {

	private MatrixService matrixService = new MatrixService();

	public String matrixAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		String col_S = (String) request.getParameter("col");
		int col = Integer.parseInt(col_S);
		request.setAttribute("row", row);
		request.setAttribute("col", col);

		Matrix matrix1 = new Matrix(row, col);
		double[][] value1 = new double[row][col];
		Matrix matrix2 = new Matrix(row, col);
		double[][] value2 = new double[row][col];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);

				name = "2_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value2[i][j] = Double.parseDouble(value);
			}
		}

		matrix1.setValue(value1);
		matrix2.setValue(value2);

		Matrix matrix3;
		try {
			matrix3 = matrixService.matrixAdd(matrix1, matrix2);
			double[][] value3 = matrix3.getValue();
			List<String> list3 = new ArrayList<String>();
			List<String> list1 = new ArrayList<String>();
			List<String> list2 = new ArrayList<String>();
			DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
			for (int i = 0; i < row; i++) {
				value = "";
				for (int j = 0; j < col; j++) {
					String str = decimalFormat.format(value3[i][j]);
					if(j == col - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				}
				list3.add(value);

				value = "";
				for (int j = 0; j < col; j++) {
					String str = decimalFormat.format(value1[i][j]);
					if(j == col - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				} 
				list1.add(value);
				
				value = "";
				for (int j = 0; j < col; j++) {
					String str = decimalFormat.format(value2[i][j]);
					if(j == col - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				} 
				list2.add(value);
			}
			request.setAttribute("matrix1", list1);
			request.setAttribute("matrix2", list2);
			request.setAttribute("matrix3", list3);
			return "f:/matrixAdd.jsp";
		} catch (MatrixException e) {
			return "f:/matrixAdd.jsp";
		}
	}	
	
	public String matrixMultiply(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row1");
		int row1 = Integer.parseInt(row_S);
		request.setAttribute("row1", row1);
		String col_S = (String) request.getParameter("col1");
		int col1 = Integer.parseInt(col_S);
		request.setAttribute("col1", col1);
		
		row_S = (String) request.getParameter("row2");
		int row2 = Integer.parseInt(row_S);
		request.setAttribute("row2", row2);
		col_S = (String) request.getParameter("col2");
		int col2 = Integer.parseInt(col_S);
		request.setAttribute("col2", col2);

		Matrix matrix1 = new Matrix(row1, col1);
		double[][] value1 = new double[row1][col1];
		Matrix matrix2 = new Matrix(row2, col2);
		double[][] value2 = new double[row2][col2];

		String name;
		String value;
		for (int i = 0; i < row1; i++) {
			for (int j = 0; j < col1; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}
		
		for (int i = 0; i < row2; i++) {
			for (int j = 0; j < col2; j++) {
				name = "2_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value2[i][j] = Double.parseDouble(value);
			}
		}

		matrix1.setValue(value1);
		matrix2.setValue(value2);

		Matrix matrix3;
		try {
			matrix3 = matrixService.matrixMultiply(matrix1, matrix2);
			int row3 = matrix3.getRow();
			int col3 = matrix3.getCol();
			double[][] value3 = matrix3.getValue();
			List<String> list3 = new ArrayList<String>();
			List<String> list1 = new ArrayList<String>();
			List<String> list2 = new ArrayList<String>();
			DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
			for (int i = 0; i < row3; i++) {
				value = "";
				for (int j = 0; j < col3; j++) {
					String str = decimalFormat.format(value3[i][j]);
					if(j == col3 - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				}
				list3.add(value);
			}
			for (int i = 0; i < row1; i++) {
				value = "";
				for (int j = 0; j < col1; j++) {
					String str = decimalFormat.format(value1[i][j]);
					if(j == col1 - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				} 
				list1.add(value);
			}
			for (int i = 0; i < row2; i++) {
				value = "";
				for (int j = 0; j < col2; j++) {
					String str = decimalFormat.format(value2[i][j]);
					if(j == col2 - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				} 
				list2.add(value);
			}
			request.setAttribute("matrix1", list1);
			request.setAttribute("matrix2", list2);
			request.setAttribute("matrix3", list3);
			return "f:/matrixMultiply.jsp";
		} catch (MatrixException e) {
			return "f:/matrixMultiply.jsp";
		}
	}
	
	
	public String matrixTranspose(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		request.setAttribute("row", row);
		String col_S = (String) request.getParameter("col");
		int col = Integer.parseInt(col_S);
		request.setAttribute("col", col);

		Matrix matrix1 = new Matrix(row, col);
		double[][] value1 = new double[row][col];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}

		matrix1.setValue(value1);

		Matrix matrix2;
		matrix2 = matrixService.matrixTranspose(matrix1);
		double[][] value2 = matrix2.getValue();
		List<String> list2 = new ArrayList<String>();
		List<String> list1 = new ArrayList<String>();
		DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
		for (int i = 0; i < col; i++) {
			value = "";
			for (int j = 0; j < row; j++) {
				String str = decimalFormat.format(value2[i][j]);
				if(j == row - 1) {
					value = value + str;
				}else {
					value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			}
			list2.add(value);
		}
		for(int i = 0;i < row;i++) {
			value = "";
			for (int j = 0; j < col; j++) {
				String str = decimalFormat.format(value1[i][j]);
				if(j == col - 1) {
					value = value + str;
				}else {
					value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			} 
			list1.add(value);
		}
		request.setAttribute("matrix1", list1);
		request.setAttribute("matrix2", list2);
		return "f:/matrixTranspose.jsp";
	}
	
	
	public String determinant(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		request.setAttribute("row", row);

		Matrix matrix1 = new Matrix(row, row);
		double[][] value1 = new double[row][row];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}

		matrix1.setValue(value1);

		DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
		
		try {
			double result_D = matrixService.determinant(matrix1);
			String result = decimalFormat.format(result_D);
			request.setAttribute("result", result);
		} catch (MatrixException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<String> list1 = new ArrayList<String>();
		for (int i = 0; i < row; i++) {
			value = "";
			for (int j = 0; j < row; j++) {
				String str = decimalFormat.format(value1[i][j]);
				if(j == row - 1) {
					value = value + str;
				}else {
					value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			}
			list1.add(value);
		}
		request.setAttribute("matrix1", list1);
		return "f:/determinant.jsp";
	}
	
	
	public String matrixInverse(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		request.setAttribute("row", row);

		Matrix matrix1 = new Matrix(row, row);
		double[][] value1 = new double[row][row];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}

		matrix1.setValue(value1);

		Matrix matrix2;
		try {
			matrix2 = matrixService.matrixInverse(matrix1);		
			double[][] value2 = matrix2.getValue();
			List<String> list2 = new ArrayList<String>();
			List<String> list1 = new ArrayList<String>();
			DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
			for (int i = 0; i < row; i++) {
				value = "";
				for (int j = 0; j < row; j++) {
					String str = decimalFormat.format(value2[i][j]);
					if(j == row - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				}
				list2.add(value);
			}
			for(int i = 0;i < row;i++) {
				value = "";
				for (int j = 0; j < row; j++) {
					String str = decimalFormat.format(value1[i][j]);
					if(j == row - 1) {
						value = value + str;
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				} 
				list1.add(value);
			}
			request.setAttribute("matrix1", list1);
			request.setAttribute("matrix2", list2);
			return "f:/matrixInverse.jsp";
		} catch (MatrixException e) {
			return "f:/matrixInverse.jsp";
		}
	}
	
	
	
	public String matrixRank(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		request.setAttribute("row", row);
		String col_S = (String) request.getParameter("col");
		int col = Integer.parseInt(col_S);
		request.setAttribute("col", col);

		Matrix matrix1 = new Matrix(row, col);
		double[][] value1 = new double[row][col];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}

		matrix1.setValue(value1);

		DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
		
		int rank = matrixService.matrixRank(matrix1);
		request.setAttribute("result", rank);
		List<String> list1 = new ArrayList<String>();
		for (int i = 0; i < row; i++) {
			value = "";
			for (int j = 0; j < col; j++) {
				String str = decimalFormat.format(value1[i][j]);
				if(j == col - 1) {
					value = value + str;
				}else {
					value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				}
			}
			list1.add(value);
		}
		request.setAttribute("matrix1", list1);
		return "f:/matrixRank.jsp";
	}
	
	
	
	public String equationsSolution(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		request.setAttribute("row", row);
		String col_S = (String) request.getParameter("col");
		int col = Integer.parseInt(col_S);
		request.setAttribute("col", col);

		Matrix matrix1 = new Matrix(row, col);
		double[][] value1 = new double[row][col];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}

		List<String> list1 = new ArrayList<String>();
		DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
		for (int i = 0; i < row; i++) {
			value = "";
			for (int j = 0; j < col; j++) {
				if(j == col - 1) {
					String str = decimalFormat.format(value1[i][j]);
					value = value + str + "x" + j;
				}else {
					String str = decimalFormat.format(value1[i][j]);
					value = value + str + "x" + j + "&nbsp;+&nbsp;";
				}
			}
			value = value + "=0";
			list1.add(value);
		}
		
		matrix1.setValue(value1);

		Matrix matrix2;
		List<String> list2 = new ArrayList<String>();
		matrix2 = matrixService.equationsSolution(matrix1);	
		if(matrix2 == null) {
			list2.add("无解！");
			request.setAttribute("matrix1", list1);
			request.setAttribute("matrix2", list2);
			return "f:/equationsSolution.jsp";
		}
		double[][] value2 = matrix2.getValue();
		row = matrix2.getRow();
		col = matrix2.getCol();
		for(int i = 0;i < row;i++) {
			value = "";
			for (int j = 0; j < col; j++) {
				String str = decimalFormat.format(value2[i][j]);
				if(j == col - 1) {
					value = value + str;
					if(i == row/2) {
						value =value + "c" + j + "&nbsp;&nbsp;";
					}
				}else {
					if(i == row/2) {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;";
						value = value + "c" + j + "&nbsp;&nbsp;";
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				}
			} 
			list2.add(value);
		}
		request.setAttribute("matrix1", list1);
		request.setAttribute("matrix2", list2);
		return "f:/equationsSolution.jsp";
	}
	
	
	public String unequationsSolution(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String row_S = (String) request.getParameter("row");
		int row = Integer.parseInt(row_S);
		request.setAttribute("row", row);
		String col_S = (String) request.getParameter("col");
		int col = Integer.parseInt(col_S);
		col++;
		request.setAttribute("col", col);

		Matrix matrix1 = new Matrix(row, col);
		double[][] value1 = new double[row][col];

		String name;
		String value;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				name = "1_" + i + "_" + j;
				value = (String) request.getParameter(name);
				value1[i][j] = Double.parseDouble(value);
			}
		}

		List<String> list1 = new ArrayList<String>();
		DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
		for (int i = 0; i < row; i++) {
			value = "";
			for (int j = 0; j < col - 1; j++) {
				if(j == col - 2) {
					String str = decimalFormat.format(value1[i][j]);
					value = value + str + "x" + j;
				}else {
					String str = decimalFormat.format(value1[i][j]);
					value = value + str + "x" + j + "&nbsp;+&nbsp;";
				}
			}
			String str = decimalFormat.format(value1[i][col - 1]);
			value = value + "=" + str;
			list1.add(value);
		}
		
		matrix1.setValue(value1);

		Matrix matrix2;
		List<String> list2 = new ArrayList<String>();
		matrix2 = matrixService.unequationsSolution(matrix1);	
		if(matrix2 == null) {
			list2.add("无解！");
			request.setAttribute("matrix1", list1);
			request.setAttribute("matrix2", list2);
			return "f:/unequationsSolution.jsp";
		}
		double[][] value2 = matrix2.getValue();
		row = matrix2.getRow();
		col = matrix2.getCol();
		for(int i = 0;i < row;i++) {
			value = "";
			for (int j = 0; j < col; j++) {
				String str = decimalFormat.format(value2[i][j]);
				if(j == col - 1) {
					value = value + str;
				}else {
					if(i == row/2) {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;";
						value = value + "c" + j + "&nbsp;&nbsp;";
					}else {
						value = value + str + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				}
			} 
			list2.add(value);
		}
		request.setAttribute("matrix1", list1);
		request.setAttribute("matrix2", list2);
		return "f:/unequationsSolution.jsp";
	}

}
